package com.qcxk.yuqian.system.utils;

import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.*;

public class ThmsUtil {
    /**
     * 判断该日期是否是该月的第一天
     *
     * @param date 需要判断的日期
     * @return
     */
    public static boolean isFirstDayOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        System.out.println(calendar.get(Calendar.MONTH));
        return calendar.get(Calendar.DAY_OF_MONTH) == 1;
    }

    /**
     * 计算两个日期之间相差的天数
     *
     * @param smdate 较小的时间
     * @param bdate  较大的时间
     * @return 相差天数
     * @throws ParseException
     */
    public static int daysBetween(Date smdate, Date bdate) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        smdate = sdf.parse(sdf.format(smdate));
        bdate = sdf.parse(sdf.format(bdate));
        Calendar cal = Calendar.getInstance();
        cal.setTime(smdate);
        long time1 = cal.getTimeInMillis();
        cal.setTime(bdate);
        long time2 = cal.getTimeInMillis();
        long between_days = (time2 - time1) / (1000 * 3600 * 24);

        return Integer.parseInt(String.valueOf(between_days));
    }

    /**
     * 　　 计算两个日期之间相差的天数
     *
     * @param smdate 较小的日期
     * @param bdate  较大的日期
     * @return 相差天数
     * @throws ParseException
     */
    public static int daysBetween(String smdate, String bdate) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.setTime(sdf.parse(smdate));
        long time1 = cal.getTimeInMillis();
        cal.setTime(sdf.parse(bdate));
        long time2 = cal.getTimeInMillis();
        long between_days = (time2 - time1) / (1000 * 3600 * 24);

        return Integer.parseInt(String.valueOf(between_days));
    }

    /**
     * 　　 计算两个时间之间相差的天数
     *
     * @param smtime 较小的日期
     * @param btime  较大的日期
     * @return 相差天数
     * @throws ParseException
     */
    public static int timesBetween(String smtime, String btime) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar cal = Calendar.getInstance();
        cal.setTime(sdf.parse(smtime));
        long time1 = cal.getTimeInMillis();
        cal.setTime(sdf.parse(btime));
        long time2 = cal.getTimeInMillis();
        long between_days = (time2 - time1) / (1000 * 3600 * 24);

        return Integer.parseInt(String.valueOf(between_days));
    }

    /**
     * 判断是否是json字符串
     *
     * @param test
     * @return
     */
    public final static boolean isJSONValid(String test) {
        try {
            JSONObject.parseObject(test);
        } catch (JSONException ex) {
            try {
                JSONObject.parseArray(test);
            } catch (JSONException ex1) {
                return false;
            }
        }
        return true;
    }

    /**
     * 获取本周的第一天
     *
     * @return String
     **/
    public static String getWeekStart() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.WEEK_OF_MONTH, 0);
        cal.set(Calendar.DAY_OF_WEEK, 2);
        Date time = cal.getTime();
        return new SimpleDateFormat("yyyy-MM-dd").format(time);
    }

    /**
     * 获取本周的最后一天
     *
     * @return String
     **/
    public static String getWeekEnd() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DAY_OF_WEEK, cal.getActualMaximum(Calendar.DAY_OF_WEEK));
        cal.add(Calendar.DAY_OF_WEEK, 1);
        Date time = cal.getTime();
        return new SimpleDateFormat("yyyy-MM-dd").format(time);
    }

    /**
     * 获取最近12个月，专门用于统计图表的X轴
     */
    public static HashMap<String, String> getLast12Months() {
        HashMap<String, String> hsmap = new HashMap<>();
        LocalDate today = LocalDate.now();
        for (long i = 0L; i < 12L; i++) {
            LocalDate localDate = today.minusMonths(i);
            String ss = localDate.toString().substring(0, 7);
            hsmap.put(ss, "0");
        }
        return hsmap;
    }

    public static String getDateAddDay(String format, int addNum) {
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(calendar.DATE, addNum);//如果把0修改为-1就代表昨天
        date = calendar.getTime();
        SimpleDateFormat format1 = new SimpleDateFormat(format);
        String dateString = format1.format(date);
        return dateString;
    }

    public static String getDateAddMin(String format, int addNum) {
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(calendar.MINUTE, addNum);//如果把0修改为-1就代表减一分钟
        date = calendar.getTime();
        SimpleDateFormat format1 = new SimpleDateFormat(format);
        String dateString = format1.format(date);
        return dateString;
    }

    public static String getDateAddHours(String format, int addNum) {
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(calendar.HOUR, addNum);//如果把0修改为-1就代表昨天
        date = calendar.getTime();
        SimpleDateFormat format1 = new SimpleDateFormat(format);
        String dateString = format1.format(date);
        return dateString;
    }

    public static String getDateAddHours(String time, String format, int addNum) {
        SimpleDateFormat format1 = new SimpleDateFormat(format);
        String dateString = null;
        try {
            Date date = format1.parse(time);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(calendar.HOUR, addNum);//如果把0修改为-1就代表昨天
            date = calendar.getTime();
            dateString = format1.format(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return dateString;
    }

    public static String getDate() {
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(calendar.DATE, 0);
        date = calendar.getTime();
        SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = format1.format(date);
        return dateString;
    }

    public static String getDate(String format) {
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(calendar.DATE, 0);
        date = calendar.getTime();
        SimpleDateFormat format1 = new SimpleDateFormat(format);
        String dateString = format1.format(date);
        return dateString;
    }

    public static boolean validateList(List list) {
        if (list != null && !list.isEmpty()) {
            return true;
        } else {
            return false;
        }
    }

    public static boolean compareTime(String time1, String time2, String format) {
        SimpleDateFormat format1 = new SimpleDateFormat(format);
        Date ten = null;
        Date now = null;
        try {
            ten = format1.parse(time1);
            now = format1.parse(time2);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return (ten.after(now));
    }

    public static String listToString(List<String> mList) {
        String convertedListStr = "";
        if (null != mList && mList.size() > 0) {
            String[] mListArray = mList.toArray(new String[mList.size()]);
            for (int i = 0; i < mListArray.length; i++) {
                if (i < mListArray.length - 1) {
                    convertedListStr += mListArray[i] + ",";
                } else {
                    convertedListStr += mListArray[i];
                }
            }
            return convertedListStr;
        } else {
            return "List is null!!!";
        }
    }

    public static String timeSubMinute(String time1, String time2, String format) {
        SimpleDateFormat format1 = new SimpleDateFormat(format);
        Date date1 = null;
        Date date2 = null;
        try {
            date1 = format1.parse(time1);
            date2 = format1.parse(time2);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        long diff = date1.getTime() - date2.getTime();
        long minute = diff / 60 / 1000;
        return Math.abs(minute) + "";
    }

    public static String timeSubDay(String time1, String time2, String format) {
        SimpleDateFormat format1 = new SimpleDateFormat(format);
        Date date1 = null;
        Date date2 = null;
        try {
            date1 = format1.parse(time1);
            date2 = format1.parse(time2);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        long diff = date1.getTime() - date2.getTime();
        long minute = diff / 60 / 1000 / 60 / 24;
        return Math.abs(minute) + "";
    }

    public static String timeSubReSec(String time1, String time2, String format) {
        SimpleDateFormat format1 = new SimpleDateFormat(format);
        Date date1 = null;
        Date date2 = null;
        try {
            date1 = format1.parse(time1);
            date2 = format1.parse(time2);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        long diff = date1.getTime() - date2.getTime();
        return (diff / 1000) + "";
    }

    /**
     * 得到N天前的日期
     */
    public static String getDataSubNum(Integer N) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String startTime = df.format(new Date().getTime() - N * 24 * 60 * 60 * 1000);
        return startTime;
    }

    /**
     * 自动判断是不是本年，是本年返回M月d日 HH:mm，不是本年返回yyyy年M月d日 HH:mm
     *
     * @param time yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static String formatDate(String time) {
        if (!StringUtils.isNotBlank(time)) {
            return "-";
        }
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat formatYear = new SimpleDateFormat("yyyy");
        SimpleDateFormat formatResult = new SimpleDateFormat("M月d日 HH:mm");
        Date date;
        String result = "";
        try {
            date = format.parse(time);
            String year = formatYear.format(date);
            String cYear = formatYear.format(new Date());
            if (!year.equals(cYear)) {//不是本年
                result = year + "年" + formatResult.format(date);
            } else {
                result = formatResult.format(date);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return result;
    }
}
